<script setup>
import { ref, toRefs, defineExpose } from "vue";
import { useDrawerStore } from "@/store/drawerDataStores.js";
import { message } from "@/utils/message";

const drawerStore = useDrawerStore();
const props = defineProps({
  devVarTableSelectedItem: {
    type: Object,
    default() {
      return {};
    }
  },
  publicForm: {
    type: Object,
    default() {
      return {};
    }
  }
});
const { devVarTableSelectedItem, publicForm } = toRefs(props);
const emit = defineEmits(["childFormObj"]);

const ruleFormRef = ref(),
  ruleForm = ref({
    url: undefined, // URL
    readingMethod: undefined, // 获取数据方法
    timeoutSetting: undefined // 超时设置
  });

// 配置按钮
// const configClick = () => {
//   ruleFormRef.value.validate(valid => {
//     if (valid) {
//       emit("childFormObj", ruleForm.value);
//     } else {
//       return false;
//     }
//   });
// };
// 配置按钮
const configClick = () => {
  // 定义一个名为 configClick 的箭头函数
  if (
    ruleForm.value.url &&
    ruleForm.value.readingMethod &&
    ruleForm.value.timeoutSetting
  ) {
    let MQTTObj = {
      URL: ruleForm.value.url, //URL
      api: ruleForm.value.readingMethod, //读取方法
      timeouts: ruleForm.value.timeoutSetting //超时设置
    };
    let SQLObj = { ...ruleForm.value };
    let FormObj = {
      MQTTObj: MQTTObj,
      SQLObj: SQLObj
    };
    emit("childFormObj", formObj); // 触发事件，传递 FormObj 对象作为参数
  } else {
    message("请填写所有的数据", { type: "warning" });
  }
};
// 父组件调用清空函数
const clearVar = () => {
  const storeForm = { ...drawerStore.mqttForm };
  ruleFormRef.value.resetFields();
  drawerStore.mqttForm = { ...storeForm };
};
// 父组件调用保存函数
const saveStore = () => {
  drawerStore.mqttForm = { ...ruleForm.value };
};
// 父组件调用赋值函数
const copyStore = () => {
  if (devVarTableSelectedItem.value.state === 1) {
    ruleForm.value = {
      url: devVarTableSelectedItem.value.url, // URL
      readingMethod: devVarTableSelectedItem.value.readingMethod, // 安全策略
      timeoutSetting: devVarTableSelectedItem.value.timeoutSetting // 超时设置
    };
  } else {
    ruleForm.value = drawerStore.mqttForm;
  }
};
defineExpose({
  clearVar,
  saveStore,
  copyStore
});
</script>

<template>
  <div class="form-main">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      label-width="auto"
      :inline="true"
      label-position="right"
    >
      <el-form-item
        label="URL"
        prop="url"
        class="form-item"
        :rules="{ required: true, message: '请输入url', trigger: 'blur' }"
      >
        <el-input v-model="ruleForm.url" placeholder="请输入url" />
      </el-form-item>
      <el-form-item
        label="数据读取方法"
        prop="readingMethod"
        class="form-item"
        :rules="{ required: true, message: '请输入url', trigger: 'change' }"
      >
        <el-select v-model="ruleForm.readingMethod">
          <el-option label="GET" value="1" />
          <el-option label="POST" value="2" />
          <el-option label="PUT" value="3" />
          <el-option label="DELETE" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item
        label="超时设置"
        prop="timeoutSetting"
        class="form-item"
        :rules="{
          required: true,
          message: '请输入超时设置',
          trigger: 'change'
        }"
      >
        <el-select v-model="ruleForm.timeoutSetting">
          <el-option label="30S" value="1" />
          <el-option label="45S" value="2" />
          <el-option label="60S" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="SSL/TLS" class="form-item">
        <div class="https-btn">
          <el-button type="primary" plain>验证</el-button>
          <el-button type="success" plain>通过</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="config-btn">
      <el-button type="primary" @click="configClick">配置</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.form-btn {
  margin-top: 30px;
}

.form-item {
  width: calc(100% - 35px);
}

.https-btn {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}

.config-btn {
  text-align: right;
  padding-top: 20px;
}
</style>
